package com.android.jay.lib_network.okhttp_learn;

import android.os.Handler;
import android.os.Looper;

import com.android.jay.lib_network.okhttp.exception.OkHttpException;
import com.android.jay.lib_network.okhttp_learn.listener.DisposeDataHandle;
import com.android.jay.lib_network.okhttp_learn.listener.DisposeDataListener;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by wzp on 2020/3/1 0001.
 * <p>
 * 描述  ：响应回调
 */
public class CommonCallBack implements Callback {

    /**
     * the logic layer exception, may alter in different app
     */
    protected final String RESULT_CODE = "ecode"; // 有返回则对于http请求来说是成功的，但还有可能是业务逻辑上的错误
    protected final int RESULT_CODE_VALUE = 0;
    protected final String ERROR_MSG = "emsg";
    protected final String EMPTY_MSG = "";
    protected final String COOKIE_STORE = "Set-Cookie"; // decide the server it


    /**
     * the java layer exception, do not same to the logic error
     */
    protected final int NETWORK_ERROR = -1; // the network relative error
    protected final int JSON_ERROR = -2; // the JSON relative error
    protected final int OTHER_ERROR = -3; // the unknow error



    private DisposeDataListener mListener;


    private Class<?> mClass;
    private Handler mHandler;


    public CommonCallBack(DisposeDataHandle handle) {

        this.mListener = handle.mListener;
        this.mClass= handle.aClass;
        mHandler = new Handler(Looper.getMainLooper());

    }

    @Override
    public void onFailure(@NotNull Call call, @NotNull final IOException e) {
             mHandler.post(new Runnable() {
                 @Override
                 public void run() {
                     mListener.onFailure(e);
                 }
             });
    }

    @Override
    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
        //子线程

        final String result = response.body().toString();
        mHandler.post(new Runnable() {
            @Override
            public void run() {
           handlerResponse(result);
            }
        });


    }

    private void handlerResponse(String result) {
        if (result == null || result.equals("")){
            mListener.onFailure(new OkHttpException(NETWORK_ERROR,EMPTY_MSG));
            return;
        }
        try {
            if (mClass == null) {
                //不需要解析直接传到应用层自己处理
                mListener.onSuccess(result);
            } else {
                //解析成为实体对象
                Object obj = new Gson().fromJson(result, mClass);
                if (obj != null) {
                    mListener.onSuccess(obj);
                } else {
                    mListener.onFailure(new OkHttpException(JSON_ERROR, EMPTY_MSG));
                }
            }
        } catch (Exception e) {
            mListener.onFailure(new OkHttpException(JSON_ERROR, e));
        }



    }
}
